package com.itheima.app.controller;

import com.itheima.app.annotation.MyLog;
import com.itheima.app.manager.VideoManager;
import com.itheima.domain.log.OperationType;
import com.itheima.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    /**
     * 查询小视频列表
     * @return
     */
    @GetMapping("/smallVideos")
    public PageBeanVo findVideo(
            @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize
    ){

        return videoManager.findVideo(pageNumber , pageSize);
    }

    /**
     * 保存小视频
     * @return
     */
    @MyLog(operationType = OperationType.PUBLISH_VIDEO)
    @PostMapping("/smallVideos")
    public Map saveVideo(MultipartFile  videoThumbnail, MultipartFile videoFile) throws IOException {

        videoManager.saveVideo(videoThumbnail , videoFile);
        return null;
    }
    /**
     * 取消关注小视频用户
     * @return
     */
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public Map deleteUserFocus(@PathVariable("uid") Long videoUserId) throws IOException {

        videoManager.deleteUserFocus(videoUserId);
        return null;
    }
    /**
     * 关注小视频用户
     * @return
     */
    @PostMapping("/smallVideos/{uid}/userFocus")
    public Map saveUserFocus(@PathVariable("uid") Long videoUserId) throws IOException {

        videoManager.saveUserFocus(videoUserId);
        return null;
    }


    /**
     *视频点赞
     */
    @MyLog(operationType = OperationType.VIDEO_LIKE)
    @PostMapping("/smallVideos/{id}/like")
    public Integer saveVideoLike(@PathVariable("id")String videoId){
        return videoManager.saveVideoLike(videoId);
    }


    /**
     * 视频取消点赞
     */
    @MyLog(operationType = OperationType.VIDEO_UNLIKE)
    @PostMapping("/smallVideos/{id}/dislike")
    public Integer dislikeVideo(@PathVariable("id")String videoId){
        return videoManager.dislikeVideo(videoId);
    }
    /**
     * 查看视频评论列表
     */
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo findVideoCommentList(@RequestParam(defaultValue = "1",name = "page") Integer pageNumber,
                                           @RequestParam(defaultValue = "10",name = "pageSize") Integer pageSize,
                                           @PathVariable("id")String videoId){
        return videoManager.findVideoCommentList(pageNumber,pageSize,videoId);
    }
    /**
     * 发布评论
     */
    @MyLog(operationType = OperationType.VIDEO_COMMENT)
    @PostMapping("/smallVideos/{id}/comments")
    public Map saveVideoComment(@RequestBody Map<String,String>map){
        String videoId = map.get("id");
        String videoComment = map.get("comment");
        return videoManager.saveVideoComment(videoId,videoComment);
    }
    /**
     * 评论点赞
     */
    @PostMapping("/smallVideos/comments/{id}/like")
    public void likeVideoComment(@PathVariable("id")String commentId){

        videoManager.likeVideoComment(commentId);

    }
    /**
     * 取消评论点赞
     */
    @PostMapping("/smallVideos/comments/{id}/dislike")
    public void dislikeVideoComment(@PathVariable("id")String commentId){

        videoManager.dislikeVideoComment(commentId);

    }
}
